#include<iostream>
#include<unistd.h>
#include<fcntl.h>
#include<sys/wait.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<string.h>
using namespace std;

int main()
{
  char buf[1024];
  int id1 = fork();
  if (id1 == 0)
  {
    int id2 = fork();

    if(id2 == 0)
    {
			int fd2;
      //cout << "子进程son1读管道FIFO1" << endl;
      if(access("FIFO1",F_OK))                                                          mkfifo("FIFO1",0644);

		  fd2 = open("FIFO1",O_RDONLY);
		  
      while(1)
      {
        read(fd2, buf, sizeof(buf));
		    cout << "Client->Server:" << buf << endl;
        if(strncmp(buf,"end",3) == 0)
          break;
      }

    }
    else 
    {
      //cout << "子进程son2写管道FIFO" << endl;
      int fd1;
      fd1 = open("FIFO",O_WRONLY);
      
      while(1)
      {
        int cnt = read(0, buf, sizeof(buf));
        write(fd1, buf, cnt);
      }

      close(fd1);
    }
  }
  else 
  {
    wait(NULL);
    wait(NULL);
  }

  return 0;
}
